<?php

namespace app\configure\controller;

use app\configure\model\ConfigPatrolItemModel;
use app\configure\model\ConfigPatrolModel;
use app\staff\model\StaffTypeModel;
use think\admin\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class Patrol extends Controller
{
    /**
     * 巡查设备管理
     * @auth true
     * @menu true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function index()
    {
        $this->title = '巡查设备管理';
//        $this->type =$this->request->param('type', 'index');
        $this->_query(ConfigPatrolModel::class)->like('name,code')->order('id desc')->page(true, true);
    }

    /**
     * 添加
     * @auth true
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function add()
    {
        $this->_form(ConfigPatrolModel::class, 'form');
    }

    /**
     * 修改
     * @auth true
     * @throws ModelNotFoundException
     * @throws DataNotFoundException
     * @throws DbException
     */
    public function edit()
    {
        $this->_applyFormToken();
        $this->_form(ConfigPatrolModel::class, 'form');
    }

    public function _form_filter(array &$data)
    {
        if ($this->request->isGet()) {
            $data['rule'] = str2arr($data['rule'] ?? '');
            $this->rule = StaffTypeModel::items1();

            $data['item'] = str2arr($data['item'] ?? '');
            $this->item = ConfigPatrolItemModel::items1();
        } elseif ($this->request->isPost()) {
            $code = $this->request->post('code') ?? shufflerCode(6) . substr(time(), 6, 3);
            $data['code'] = $code;

            $data['rule'] = arr2str($data['rule'] ?? []);
            $data['item'] = arr2str($data['item'] ?? []);
        }
    }

    /**
     * 删除
     * @auth true
     * @throws DbException
     */
    public function remove()
    {
        $this->_delete(ConfigPatrolModel::class);
    }
}